package com.sq.activity.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sq.common.core.domain.AjaxResult;
import com.sq.common.core.domain.entity.SysRole;
import com.sq.common.core.domain.entity.SysUser;
import com.sq.common.utils.DateUtils;
import com.sq.common.utils.UserThreadLocal;
import com.sq.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sq.activity.mapper.ActivityPublisherApplyMapper;
import com.sq.activity.domain.ActivityPublisherApply;
import com.sq.activity.service.IActivityPublisherApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 申请信息Service业务层处理
 * 
 * @author shan
 * @date 2025-02-12
 */
@Service
public class ActivityPublisherApplyServiceImpl extends ServiceImpl<ActivityPublisherApplyMapper, ActivityPublisherApply> implements IActivityPublisherApplyService
{
    @Autowired
    private ActivityPublisherApplyMapper activityPublisherApplyMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询申请信息
     * 
     * @param id 申请信息主键
     * @return 申请信息
     */
    @Override
    public ActivityPublisherApply selectActivityPublisherApplyById(Long id) {
        return activityPublisherApplyMapper.selectonId(id);
    }

    /**
     * 查询申请信息列表
     * 
     * @param activityPublisherApply 申请信息
     * @return 申请信息
     */
    @Override
    public List<ActivityPublisherApply> selectActivityPublisherApplyList(ActivityPublisherApply activityPublisherApply) {
        List<ActivityPublisherApply> list = activityPublisherApplyMapper.selectActivityPublisherApplyListByUserId(UserThreadLocal.getUserId());

        //判断当前用户的权限
        SysUser sysUser = sysUserMapper.selectUserById(UserThreadLocal.getUserId());
        if (sysUser.getStatus().equals("0")) {
            List<SysRole> roles = sysUser.getRoles();
            for (SysRole role : roles) {
                if(role.getStatus().equals("0") && role.getRoleKey().equals("common")){
                    //普通用户只能查看自己的申请信息
                    return list;
                }else if (role.getStatus().equals("0") && role.getRoleKey().equals("admin")){
                    //管理员可以查看所有申请信息
                    list = activityPublisherApplyMapper.selectActivityPublisherApplyList(activityPublisherApply);
                }
            }
        }
        return list;
    }

    /**
     * 新增申请信息
     * 
     * @param activityPublisherApply 申请信息
     * @return 结果
     */
    @Override
    public int insertActivityPublisherApply(ActivityPublisherApply activityPublisherApply) {
        //判断申请人是否已经存在
        SysUser sysUser = sysUserMapper.selectUserByUserNickName(activityPublisherApply.getSysUserNickName());
        if(ObjectUtils.isEmpty(sysUser)){
            return 0;
        }
        activityPublisherApply.setSysUserId(sysUser.getUserId());

        return activityPublisherApplyMapper.insertActivityPublisherApply(activityPublisherApply);
    }

    /**
     * 修改申请信息
     * 
     * @param activityPublisherApply 申请信息
     * @return 结果
     */
    @Override
    public int updateActivityPublisherApply(ActivityPublisherApply activityPublisherApply) {

        return activityPublisherApplyMapper.updateActivityPublisherApply(activityPublisherApply);
    }

    /**
     * 批量删除申请信息
     * 
     * @param ids 需要删除的申请信息主键
     * @return 结果
     */
    @Override
    public int deleteActivityPublisherApplyByIds(Long[] ids) {
        return activityPublisherApplyMapper.deleteActivityPublisherApplyByIds(ids);
    }

    /**
     * 删除申请信息信息
     * 
     * @param id 申请信息主键
     * @return 结果
     */
    @Override
    public int deleteActivityPublisherApplyById(Long id)
    {
        return activityPublisherApplyMapper.deleteActivityPublisherApplyById(id);
    }

    @Override
    public int selectApplyStatus(Long userId) {
        return activityPublisherApplyMapper.selectApplyStatus(userId);
    }
}
